﻿using ConsoleApp7.Filter;
using ConsoleApp7.Server;
using Dapper;
using IRealize;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Data.SqlClient;
using Realize;

namespace WebApplication9.Controllers.Recruit_Manage.JL
{
    public class ER_AddController : Controller
    {

        private readonly IConfiguration configuration;
        private IWebHostEnvironment _environment;

        public ER_AddController(IConfiguration configuration, IWebHostEnvironment environment)
        {
            this.configuration = configuration;
            _environment = environment;
        }        

        public IActionResult Index()
        {
            return View();
        }

        [HttpGet("FirstLevel2")]
        public async Task<IActionResult> FirstLevel2()
        {
            using (var connection = new SqlConnection(configuration["SqlServer"]))
            {
                await connection.OpenAsync();

                // 查询I级机构
                var firstKinds = await connection.QueryAsync<dynamic>(
                    "SELECT major_kind_id AS id, major_kind_name AS name FROM config_major_kind");

                // 构建级联数据结构
                var result = new List<dynamic>();

                foreach (var firstKind in firstKinds)
                {
                    // 查询对应的II级机构
                    var secondKinds = await connection.QueryAsync<dynamic>(
                        "SELECT major_id AS id, major_name AS name " +
                        "FROM config_major " +
                        "WHERE major_kind_id = @Id",
                        new { Id = firstKind.id });

                    var secondList = new List<dynamic>();

                    foreach (var secondKind in secondKinds)
                    {

                        secondList.Add(new
                        {
                            secondKind.id,
                            secondKind.name,
                            
                        });
                    }

                    result.Add(new
                    {
                        firstKind.id,
                        firstKind.name,
                        children = secondList
                    });
                }

                return Ok(result);
            }
        }

        [HttpPost]
        public IActionResult WJSC(IFormFileCollection file)
        {
            string path = "";
            foreach (var item in file)
            {
                //生成文件名
                string name = DateTime.Now.ToString("yyyyMMddHHmmssfff");
                //获取后缀名
                string ext = item.FileName.Substring(item.FileName.LastIndexOf('.'));
                //设置上传路径
                path = $"Uploader/{DateTime.Now.ToString("yyyy/MM/dd/")}" + name + ext;
                //生成文件的绝对路径
                var uploads = Path.Combine(_environment.WebRootPath, path);
                Console.WriteLine("1:"+uploads);
                Console.WriteLine("2:"+ path);
                //创建上传文件对应的文件夹
                (new FileInfo(uploads)).Directory.Create();
                using (var stream = new FileStream(uploads, FileMode.CreateNew))
                {
                    item.CopyTo(stream);//图片上传
                    //TempData["name"] = name + ext;//保存图片的值
                }
            }
            return Content("/"+path);
        }

        [HttpGet]
        public async Task<IActionResult> Add(
                    string HumanName,//姓名
                    string HumanEmail,//email
                    string HumanTelephone,//电话号码
                    string HumanHomephone,//家庭电话
                    string HumanMobilephone,//手机
                    string HumanAddress,//地址
                    string HumanPostcode,//邮编
                    string HumanBirthplace,//出生地
                    string HumanBirthday,//生日
                    string HumanIdcard,//请输入身份证号码
                    int HumanAge,//年龄
                    string HumanCollege,//薪酬要求
                    string DemandSalaryStandard,//毕业院校
                    string RegisterTime,//注册时间
                    string HumanHistoryRecords,//个人履历
                    string Remark,//备注
                    string HumanMajorKindId,//职位分类编号
                    string HumanMajorKindName,//职位分类名称
                    string HumanMajorId,//职位编号
                    string HunmaMajorName, //职位名称
                    string value1 , //招聘类型
                    string value2 , //性别
                    string value3 , //国际
                    string value4 , //民族
                    string value5 , //宗教信仰
                    string value6 , //政治面貌
                    string value7 , //爱好
                    int value8 , //教育年限
                    string value9 , //学历
                    string value10, //学历专业
                    string value11, //特长
                    string image //图片路径
            )
        {
            IER er = new ER();
            int result = await er.Add(HumanName, HumanEmail, HumanTelephone, HumanHomephone, HumanMobilephone, HumanAddress, HumanPostcode, HumanBirthplace, HumanBirthday, HumanIdcard,
                HumanAge, HumanCollege, DemandSalaryStandard, RegisterTime, HumanHistoryRecords, Remark, HumanMajorKindId, HumanMajorKindName, HumanMajorId, HunmaMajorName,
                value1, value2, value3, value4, value5, value6, value7, value8, value9, value10, value11, image, configuration["SqlServer"]);

            if (result > 0)
            {
                return Ok(new 
                {
                    zhi = "ok"
                });
            }
            else
            {
                return Ok(new
                {
                    zhi = "nook"
                });
            }
            

            
        }

    }
}
